package com.runfast.paotui.web.controller.server;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.paotui.dao.model.Activity;
import com.runfast.paotui.service.ActivityService;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentbusiness;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.utils.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * ${DESCRIPTION}活动管理
 *
 * @author chenzheng
 * @create 2018-05-08 21:20
 **/
@RestController("paotuiActivityController")
@RequestMapping("/server/paotui/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    /**
     * 活动列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, Activity t) throws ParseException{
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        Map<Object,Object> map=new HashMap<Object, Object>();
        Map<Object,Object> map2=new HashMap<Object, Object>();
        List<Activity> list=new ArrayList<Activity>();
        Integer total=null;
        map2.put("pageable",pageable);
        if(t!=null){
            /**活动类型*/
            if(t.getPtype()!=null){
                map2.put("pType",t.getPtype());
            }
            if(StringUtils.isNotBlank(t.getName())){
                map2.put("name",t.getName());
            }
            if(t.getTimeType()!=null){
                map2.put("timeType",t.getTimeType());
            }
        }
        if(admin!=null && admin.getType()==2  && agentAccounts!=null){
            //代理商
            map2.put("agentIds",agentAccounts.getAgentid());
        }else if(admin !=null  && admin.getType()==1){
            //后台管理员---查看全部的
            if(StringUtils.isNotBlank(t.getAgentIds())){
                map2.put("agentIds",t.getAgentIds());
            }
        }
        list=activityService.selectByMapWithPageable(map2);
        total=activityService.countByMap(map2);
        if(list.size()>0){
            for(Activity activity:list){
                setActivity1(activity);
            }
        }
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 保存活动
     */
    @PostMapping("/save")
    public Result save(@RequestBody Activity activity) throws ParseException {
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(activity!=null && StringUtils.isNotBlank(activity.getName()) && StringUtils.isNotBlank(activity.getStartTimeToendTime())){
            if(activity!=null && activity.getPtype()!=null){
                if(admin!=null  && admin.getType()==2 && agentAccounts!=null){
                    //代理商登录
                    activity.setCreateby(agentAccounts.getAgentid());
                    activity.setCreatetype(1);
                }else if(admin!=null &&admin.getType()==1){
                    // 后台用户登录
                    activity.setCreateby(admin.getId());
                    activity.setCreatetype(2);
                }
                activity.setStops(0);
                activity.setCreatetime(new Date());
                activity.setDeleted(0);
                activity.setDiscard(false);
                setActivity(activity);
                if(activity.getPtype()==9){
                    //新用户立减活动
                    if(activity.getAgentid()!=null && activity.getLesss()!=null){
                        WaiMaiRunfastAgentbusiness  agentbusiness =waiMaiRunfastAgentbusinessService.setAgent(activity.getAgentid());
                        if(agentbusiness!=null){
                            activity.setAgentname(agentbusiness.getName());
                            activity.setAgentid(agentbusiness.getId());
                        }else{return Result.fail(null,"找不到对应的代理商！");}
                        activityService.insertSelective(activity);
                    }else{return Result.fail(null,"传入参数不对！");}
                }else if(activity.getPtype()==6){
                    //跑腿优惠券
                    if(activity.getAgentid()!=null  &&activity.getFulls()!=null && activity.getShared()!=null && activity.getRedamount()!=null && activity.getRedlimittype()!=null && activity.getRednum()!=null && activity.getRedpersonlimittype()!=null && activity.getRedusertype()!=null){
                        WaiMaiRunfastAgentbusiness agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(activity.getAgentid());
                        if(agentbusiness!=null){
                            activity.setAgentid(agentbusiness.getId());
                            activity.setAgentname(agentbusiness.getName());
                            activityService.insertSelective(activity);
                        }else{return Result.fail(null,"找不到对应的代理商！");}
                    }else{return Result.fail(null,"传入参数不对！");}
                }
            }else{return Result.fail(null,"活动类型不能为空！");}
        }else{return Result.fail(null,"传入参数不对！");}
        //Map<Object,Object> map=new HashMap<Object, Object>();
        //map.put("activityId",activity.getId());
        //return Result.ok(null,map);
        return Result.ok(null);
    }




    /**前端传给后台数据转换--营业时间*/
    public void setActivity(Activity activity)  throws ParseException{
        if(StringUtils.isNotBlank(activity.getStartTimeToendTime())){
            List<String> result = Arrays.asList(StringUtils.split(activity.getStartTimeToendTime(),","));
            if(result.size()>0){
                activity.setStarttime(TimeUtil.getDate(result.get(0)));
                activity.setEndtime(TimeUtil.getDate(result.get(1)));
            }
        }else{
            activity.setStarttime(null);
            activity.setEndtime(null);
        }
    }


    /**后台传给前台数据转换*/
    public void setActivity1(Activity activity)  throws ParseException{
        if(activity.getStarttime()!=null && activity.getEndtime()!=null){
            activity.setStartTimeToendTime(TimeUtil.getDateToString(activity.getStarttime())+","+TimeUtil.getDateToString(activity.getEndtime()));
        }
    }
    /**活动详情*/
    @GetMapping("/info")
    public Result info(Integer activityId) throws ParseException{
        if(activityId==null){return Result.fail(null,"没有活动id！");}
        Activity activity=activityService.selectByPrimaryKey(activityId);
        if(activity!=null){
            if(activity.getAgentid()!=null){
                WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(activity.getAgentid());
                if(agentbusiness!=null){
                    activity.setAgentname(agentbusiness.getName());
                }
            }
            setActivity1(activity);
        }else{return Result.fail(null,"找不到对应的活动！");}
        return Result.ok(null,activity);
    }

     /**活动作废*/
     @GetMapping("/discard")
     public Result discard(Activity activity){
         if(activity!=null && activity.getId()!=null){
             Activity activity1 = activityService.selectByPrimaryKey(activity.getId());
             if(activity1!=null){
                 activity1.setDiscard(true);
                 activity1.setDiscardtime(new Date());
                 activityService.updateByPrimaryKeySelective(activity1);
             }else{return Result.fail(null,"未找到对应的活动！");}
         }else{return Result.fail(null,"传入参数出错！");}
       return Result.ok(null);
     }

     /**活动删除*/
     @GetMapping("/delete")
     public Result delete(Activity activity){
         if(activity!=null && activity.getId()!=null){
             Activity activity1 = activityService.selectByPrimaryKey(activity.getId());
             if(activity1!=null){
                 activity1.setDeleted(1);
                 activityService.updateByPrimaryKeySelective(activity1);
             }else{return Result.fail(null,"未找到对应的活动！");}
         }else{return Result.fail(null,"传入参数出错！");}
         return Result.ok(null);
     }



}
